package 每日一题;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/22 14:41
 */
public class L1027 {
    public static void main(String[] args) {
        new L1027().longestArithSeqLength(new int[]{2,4,6,8});
    }
    public int longestArithSeqLength(int[] nums) {
        if (nums.length == 2)
            return 2;
        int ans = 0;
        //简单优化一下
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(set.contains(nums[j] - nums[i]))
                    continue;
                set.add(nums[j] - nums[i]);
                int temp = 2 + dfs(nums, j + 1, nums[j] - nums[i], nums[j]);
                ans = Math.max(ans, temp);
            }
            set.clear();
        }
        return ans;
    }

    public int dfs(int[] nums, int startIndex, int x, int pre) {
        if (startIndex >= nums.length)
            return 0;
        for (int i = startIndex; i < nums.length; i++) {
            if (nums[i] - pre == x) {
                return 1 + dfs(nums, i + 1, x, nums[i]);
            }
        }
        return 0;
    }
}
